/**
 *
 */
package cn.hg.jfsa.manage.controller.system.validator;

import com.jfinal.core.Controller;

import cn.hg.base.jfinal.validator.BaseValidator;
import cn.hg.base.kit.PasswordKit;
import cn.hg.jfsa.core.SessionThreadLocal;
import cn.hg.jfsa.model.JfsaAdmin;

/**
 * @author Golly
 *
 */
public class AdminValidator extends BaseValidator {

	/*
	 * (non-Javadoc)
	 *
	 * @see com.jfinal.validate.Validator#handleError(com.jfinal.core.Controller)
	 */
	@Override
	protected void handleError(Controller c) {

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.jfinal.validate.Validator#validate(com.jfinal.core.Controller)
	 */
	@Override
	protected void validate(Controller c) {
		this.validateString("admin.account", 2, 20, "accountMsg", "账号必须填写2到20长度的字符！");
		this.validateString("admin.name", 2, 20, "nameMsg", "昵称必须填写2到20长度的字符！");

		// 如果要修改密码，必须输入当前管理员的登录密码并验证
		if (!c.isParaBlank("admin.id") && !c.isParaBlank("newPassword")) {
			this.validateString("newPassword", 6, 20, "newPasswordMsg", "新密码必须填写6到20长度的字符!");
			this.validateEqualField("newPassword", "confirmPassword", "newPasswordMsg", "确认密码必须和新密码相同！");
			this.validateRequired("selfPassword", "selfPasswordMsg", "修改密码时必须填写当前操作员密码！");

			JfsaAdmin currAdmin = SessionThreadLocal.getAdmin();
			if (!PasswordKit.validatePassword(currAdmin.getSalt(), c.getPara("selfPassword"),
					currAdmin.getPassword())) {
				this.addError("selfPasswordMsg", "当前操作员密码不正确！");
			}
		}

		this.validateRequired("admin.status", "statusMsg", "必须填写状态！");
		this.validateInteger("admin.orgId", 1, Integer.MAX_VALUE, "orgMsg", "必须填写组织机构！");

		if (!c.isParaBlank("admin.email")) {
			this.validateEmail("admin.email", "emailMsg", "电子邮件格式不正确！");
		}
		if (!c.isParaBlank("admin.mobilePhone")) {
			this.validateMobilePhone("admin.mobilePhone", "mobilePhoneMsg", "手机号码格式不正确！");
		}
		if (!c.isParaBlank("admin.telephone")) {
			this.validateTelePhone("admin.telephone", "telephoneMsg", "电话号码格式不正确！");
		}
	}

}
